package org.phoenixframework.api.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.phoenixframework.api.search.mapper.PmsProductMapper;
import org.phoenixframework.api.search.repository.EsPmsProductRepository;
import org.phoenixframework.api.search.service.IPmsProductService;
import org.phoenixframework.common.entity.pms.PmsProduct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * pms-商品信息 服务实现类
 * </p>
 *
 * @author doublelife
 * @since 2021-07-18
 */
@AllArgsConstructor
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements IPmsProductService {

    private final EsPmsProductRepository pmsProductRepository;

    /**
     * 从数据库中导入所有商品到ES
     */
    @Override
    public void importAll() {
        List<PmsProduct> pmsProducts = baseMapper.selectList(new QueryWrapper<>());
        pmsProductRepository.saveAll(pmsProducts);
    }

    /**
     * 根据关键字搜索名称
     *
     * @param keyword
     * @param pageable
     */
    @Override
    public Page<PmsProduct> search(String keyword, Pageable pageable) {
        return pmsProductRepository.findByName(keyword, pageable);
    }
}
